package br.com.evl.tecinfo.gui;

import br.com.evl.tecinfo.dao.Cargo_Banco;
import br.com.evl.tecinfo.modelo.Cargo;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Tecinfo
 */
public class TelaCadrastroCargo extends javax.swing.JFrame {

    int editar = 0;

    public TelaGeral telaGeral;

    private Cargo novoCargo;
    private Cargo_Banco cargoBD;

    public TelaCadrastroCargo() {

        initComponents();

        novoCargo = new Cargo();
        cargoBD = new Cargo_Banco();

        atualizarTabela();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        JT_Table = new javax.swing.JTable();
        jB_remover = new javax.swing.JButton();
        jB_editar1 = new javax.swing.JButton();
        jBcadastrar_tc1 = new javax.swing.JButton();
        JB_voltar = new javax.swing.JButton();
        JTnome_cargo = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        JL_text_no = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastrar cargo");
        setMinimumSize(new java.awt.Dimension(600, 600));
        setResizable(false);
        getContentPane().setLayout(null);

        JT_Table.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        JT_Table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome do cargo"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        JT_Table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(JT_Table);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(40, 240, 500, 280);

        jB_remover.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jB_remover.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/icone_excluirv2.png"))); // NOI18N
        jB_remover.setText("Remover");
        jB_remover.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jB_remover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_removerActionPerformed(evt);
            }
        });
        getContentPane().add(jB_remover);
        jB_remover.setBounds(350, 550, 120, 40);

        jB_editar1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jB_editar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/icon_editar1v2.png"))); // NOI18N
        jB_editar1.setText("Editar");
        jB_editar1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jB_editar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_editar1ActionPerformed(evt);
            }
        });
        getContentPane().add(jB_editar1);
        jB_editar1.setBounds(220, 550, 120, 40);

        jBcadastrar_tc1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jBcadastrar_tc1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/icone_add1v2.png"))); // NOI18N
        jBcadastrar_tc1.setText("Cadastrar");
        jBcadastrar_tc1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jBcadastrar_tc1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBcadastrar_tc1ActionPerformed(evt);
            }
        });
        getContentPane().add(jBcadastrar_tc1);
        jBcadastrar_tc1.setBounds(90, 550, 120, 40);

        JB_voltar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/voltar.png"))); // NOI18N
        JB_voltar.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        JB_voltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_voltarActionPerformed(evt);
            }
        });
        getContentPane().add(JB_voltar);
        JB_voltar.setBounds(540, 10, 50, 40);

        JTnome_cargo.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        JTnome_cargo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JTnome_cargoActionPerformed(evt);
            }
        });
        getContentPane().add(JTnome_cargo);
        JTnome_cargo.setBounds(140, 160, 380, 30);

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/logomarcav2.png"))); // NOI18N
        getContentPane().add(jLabel4);
        jLabel4.setBounds(10, 0, 140, 110);

        JL_text_no.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        JL_text_no.setText("Nome do cargo:");
        getContentPane().add(JL_text_no);
        JL_text_no.setBounds(40, 160, 100, 20);

        jLabel1.setFont(new java.awt.Font("Trebuchet MS", 1, 30)); // NOI18N
        jLabel1.setText("Cadastro Cargo");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(190, 30, 240, 40);

        jLabel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        getContentPane().add(jLabel2);
        jLabel2.setBounds(40, 110, 500, 120);

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/back_azul.jpg"))); // NOI18N
        jLabel3.setMaximumSize(new java.awt.Dimension(600, 600));
        getContentPane().add(jLabel3);
        jLabel3.setBounds(0, 0, 600, 600);

        setSize(new java.awt.Dimension(616, 638));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void JTnome_cargoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JTnome_cargoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_JTnome_cargoActionPerformed

    private void jB_removerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_removerActionPerformed

        Cargo removeCargo = new Cargo();

        removeCargo.setNome((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 0));

        cargoBD.remover(removeCargo);

        atualizarTabela();

    }//GEN-LAST:event_jB_removerActionPerformed

    private void jB_editar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_editar1ActionPerformed

        if (editar == 0) {

            JTnome_cargo.setText((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 0));
            jB_editar1.setText("Concluir");
            jB_remover.setEnabled(false);
            jBcadastrar_tc1.setEnabled(false);
            JT_Table.setEnabled(false);
            editar = 1;

        } else {

            jB_removerActionPerformed(evt);
            jBcadastrar_tc1ActionPerformed(evt);

            jB_editar1.setText("Editar");
            jB_remover.setEnabled(true);
            jBcadastrar_tc1.setEnabled(true);
            JT_Table.setEnabled(true);
            editar = 0;

        }


    }//GEN-LAST:event_jB_editar1ActionPerformed

    private void jBcadastrar_tc1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBcadastrar_tc1ActionPerformed

        novoCargo.setNome(JTnome_cargo.getText());

        cargoBD.inserir(novoCargo);

        atualizarTabela();

    }//GEN-LAST:event_jBcadastrar_tc1ActionPerformed

    private void JB_voltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_voltarActionPerformed

        this.setVisible(false);


    }//GEN-LAST:event_JB_voltarActionPerformed

    private void atualizarTabela() {

        cargoBD = new Cargo_Banco();

        DefaultTableModel dtm = new DefaultTableModel(null, new String[]{"Nome do cargo"});

        try {
            while (cargoBD.consulta.next()) {

                System.out.println(cargoBD.consulta.getString("nome"));
                
            }
        } catch (SQLException ex) {
            Logger.getLogger(TelaCadastroFormaPagamento.class.getName()).log(Level.SEVERE, null, ex);
        }
        JT_Table.setModel(dtm);
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JB_voltar;
    private javax.swing.JLabel JL_text_no;
    private javax.swing.JTable JT_Table;
    private javax.swing.JTextField JTnome_cargo;
    private javax.swing.JButton jB_editar1;
    private javax.swing.JButton jB_remover;
    private javax.swing.JButton jBcadastrar_tc1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
